package com.portico.dept.solarch

class ClientProjectController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    static scaffold = true ; 

    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [clientProjectInstanceList: ClientProject.list(params), clientProjectInstanceTotal: ClientProject.count()]
    }

    def create = {
        def clientProjectInstance = new ClientProject()
        clientProjectInstance.properties = params
        return [clientProjectInstance: clientProjectInstance]
    }

    def save = {
        def clientProjectInstance = new ClientProject(params)
        if (clientProjectInstance.save(flush: true)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'clientProject.label', default: 'ClientProject'), clientProjectInstance.id])}"
            redirect(action: "show", id: clientProjectInstance.id)
        }
        else {
            render(view: "create", model: [clientProjectInstance: clientProjectInstance])
        }
    }

    def show = {
        def clientProjectInstance = ClientProject.get(params.id)
        if (!clientProjectInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'clientProject.label', default: 'ClientProject'), params.id])}"
            redirect(action: "list")
        }
        else {
            [clientProjectInstance: clientProjectInstance]
        }
    }

    def edit = {
        def clientProjectInstance = ClientProject.get(params.id)
        if (!clientProjectInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'clientProject.label', default: 'ClientProject'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [clientProjectInstance: clientProjectInstance]
        }
    }

    def update = {
        def clientProjectInstance = ClientProject.get(params.id)
        if (clientProjectInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (clientProjectInstance.version > version) {
                    
                    clientProjectInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'clientProject.label', default: 'ClientProject')] as Object[], "Another user has updated this ClientProject while you were editing")
                    render(view: "edit", model: [clientProjectInstance: clientProjectInstance])
                    return
                }
            }
            clientProjectInstance.properties = params
            if (!clientProjectInstance.hasErrors() && clientProjectInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'clientProject.label', default: 'ClientProject'), clientProjectInstance.id])}"
                redirect(action: "show", id: clientProjectInstance.id)
            }
            else {
                render(view: "edit", model: [clientProjectInstance: clientProjectInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'clientProject.label', default: 'ClientProject'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def clientProjectInstance = ClientProject.get(params.id)
        if (clientProjectInstance) {
            try {
                clientProjectInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'clientProject.label', default: 'ClientProject'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'clientProject.label', default: 'ClientProject'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'clientProject.label', default: 'ClientProject'), params.id])}"
            redirect(action: "list")
        }
    }
}
